<?php


namespace app\api\controller\user;

use app\api\controller\Common;
class Address extends Common
{
    protected $data;
    public function initialize()
    {
        parent::initialize();
        $this->checkUserLogin();
        $this->data = $this->request->except(['sign','accessKey','token']);
    }

    /**
     * 地址列表
     */
    public function list(){
        $uid = $this->user['id'];
        $list = db('users_address')->alias('ua')
            ->field('ua.id,ua.name,ua.mobile,ua.address,ua.default,ua.province,ua.city,ua.district')
            // ->field('r1.name province_name,r2.name city_name,r3.name district_name')
            // ->leftJoin('region r1','ua.province = r1.id')
            // ->leftJoin('region r2','ua.city = r2.id')
            // ->leftJoin('region r3','ua.district = r3.id')
            ->where('uid',$uid)
            ->order('default')
            ->order('id', 'desc')
            ->select();
        $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功','data'=>$list];
        return $this->show($this->return);
    }

    /**
     * 添加/修改地址
     */
    public function update(){
        $uid = $this->user['id'];
        $id = $this->data['id'];
        $name = $this->data['name'];
        $mobile = $this->data['mobile'];
        $province = input('province',0);
        $city = input('city',0);
        $district = input('district',0);
        $address = $this->data['address'];
        $default = $this->data['default'] ? $this->data['default'] : 2;
        if($default ==1){
            db('users_address')->where('uid', $uid)->update(['default'=>2]);
        }
        $data = [
            'uid' => $uid,
            'name' => $name,
            'mobile' => $mobile,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'address' => $address,
            'default' => $default,
        ];
        $count = db('users_address')->where('uid',$uid)->count();
        if($count == 0){ // 首次添加，设为默认
            $data['default'] = 1;
        }else{
            if($default == 1){ // 只能有一个默认地址
                db('users_address')->where('uid',$uid)->update(['default'=>2]);
            }
        }
        if($id){
            $res = db('users_address')->where('id',$id)->update($data);
        }else{
            $res = db('users_address')->insert($data);
        }
        if($res){
            $this->return = ['status'=>200,'code'=>'SUCCESS','message'=>'成功'];
        }else{
            $this->return = ['status'=>200,'code'=>'GET_ERROR','message'=>'失败'];
        }
        return $this->show($this->return);
    }

    /**
     * 添加/修改地址
     */
    public function delete()
    {
        $uid = $this->user['id'];
        $id = $this->data['id'];

        $res = db('users_address')->where('id', $id)->delete();
        
        if ($res) {
            $this->return = ['status' => 200, 'code' => 'SUCCESS', 'message' => '成功'];
        } else {
            $this->return = ['status' => 200, 'code' => 'GET_ERROR', 'message' => '失败'];
        }
        return $this->show($this->return);
    }
}